package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author zhouxiangyang
 * @Date 2021/7/12 9:43
 * 用于处理和用户相关的请求
 */
@Controller
@RequestMapping("/user")  //一级路径
public class UserController {

    /**
     * 处理保存用户的请求
     * @return 表示访问完该方法之后要跳转的页面
     */
    @RequestMapping("/save")//二级路径  //http://localhost:8080/springmvc_01/user/save
    public String save(){
        System.out.println("UserController save方法被访问了...");
        return "/success.jsp";
    }

    //http://localhost:8080/springmvc_01/user/requestParam1?name=snake&age=20
    @RequestMapping("/requestParam1")//二级路径
    public String requestParam1(String name,Integer age){
        System.out.println(name+","+age);
        return "/success.jsp";
    }

    //http://localhost:8080/springmvc_01/user/requestParam2?username=snake&age=20
    @RequestMapping("/requestParam2")//二级路径
    public String requestParam2(@RequestParam("username") String name, Integer age){
        System.out.println(name+","+age);
        return "/success.jsp";
    }


    //http://localhost:8080/springmvc_01/user/requestParam3?name=snake&age=20&nick=张益达&nick=张大炮
    @RequestMapping("/requestParam3")//二级路径
    public String requestParam3(User user){
        System.out.println(user);
        return "/success.jsp";
    }

    //http://localhost:8080/springmvc_01/user/requestParam4?nick=张益达&nick=张大炮
    @RequestMapping("/requestParam4")//二级路径
    public String requestParam4(String[] nick){
        System.out.println(Arrays.toString(nick));
        return "/success.jsp";
    }

    //http://localhost:8080/springmvc_01/user/requestParam5?nick=张益达&nick=张大炮
    @RequestMapping("/requestParam5")//二级路径
    public String requestParam5(@RequestParam("nick") List<String> list){
        System.out.println(list);
        return "/success.jsp";
    }

    @RequestMapping("/requestParam6")
    public String requestParam6(@DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday){
        System.out.println("birthday = " + birthday);
        return "/success.jsp";
    }


    //---------------------以下是响应json数据的代码【重要】----------------
    @RequestMapping(value = "/responseString",produces = "text/html;charset=utf-8")
    @ResponseBody //作用：不进行页面跳转，通过响应体响应数据。
    public String responseString(){
        return "hello 张益达!";
    }

    @RequestMapping(value = "/responseUserJson")
    @ResponseBody
    public User responseUserJson() throws JsonProcessingException {
        User user=new User();   //实际开发是调用service层方法得到的user对象
        user.setName("张益达");
        user.setAge(22);
        List<String> list = Stream.of("snake", "金甲战士", "张大炮").collect(Collectors.toList());
        user.setNick(list);
        //将user对象转换成json字符串，响应给客户端
        return user;
    }

    @RequestMapping(value = "/responseListJson")
    @ResponseBody
    public List<User> responseListJson() throws JsonProcessingException {
        User user=new User();   //实际开发是调用service层方法得到的user对象
        user.setName("张益达");
        user.setAge(22);
        List<String> list = Stream.of("snake", "金甲战士", "张大炮").collect(Collectors.toList());
        user.setNick(list);

        //将List集合对象转换成json字符串，响应给客户端
        List<User> users = Stream.of(user, user, user).collect(Collectors.toList());
        return users;
    }
}
